
<!doctype html>
<html>
  <head></head>
  <script language="javascript" type="text/javascript" src="../js/js/baseJs.js"></script>
  <meta charset="UTF-8">
  <meta content="user-scalable=no,width=device-width, initial-scale=1" id="viewport" name="viewport" initial-scale="1">
<body>

<script src="../js/js/rem_fix.js"></script>
<script src="../js/js/common.js"></script>
<script type="text/javascript">
var projectId = parseInt(window.location.hash.split("#")[1]);
var loginUrl = "../userBundle/login.mobile.html" + "?" + "../userBundle/applayPos.mobile.html" + "#" + projectId;
var token = getCookie("userToken");
var userId = getCookie("userId");
if (!token || !userId) {
	window.location.href = loginUrl;
}
var userData,upload_qiniu_addr,qiniuData={key: "",token: ""},isApplyProjectItem;
var payImage=[];var projectItems=[];
var projectUserId
window.onload = function(){
	getProject()
	getUserData();
	getQiniuToken();
	getQiniuImageUrl();
	document.getElementById('upload').addEventListener('change', function (event) {
		var reader = new FileReader();
		reader.readAsDataURL(event.target.files[0]); //将读取到的文件编码成Data URL
		var currentImgTempArray = [];
		if (currentImgTempArray.length >= 5) {
			alert("最多上传5张图片");
			return false;
		}
		reader.onload = function() { //读取完成时
			var replaceSrc = reader.result; //文件输出的内容
			//console.log(reader)
			//压缩图片处理
			var image = new Image();
			image.src = replaceSrc;
			image.onload = function() {
				//获取图片初始宽高
				var width = image.width;
				var height = image.height;
				//判断图片宽度，再按比例设置宽度和高度的值
				if (width > 1024) {
					width = 1024;
					height = Math.ceil(1024 * (image.height / image.width));
				}
		
				//将图片重新画入canvas中
				var canvas = document.getElementById("compressCanvas");
				var context = canvas.getContext("2d");
				canvas.width = width;
				canvas.height = height;
				context.beginPath();
				context.fillStyle = "#ffffff";
				context.fillRect(0, 0, width, height);
				context.fill();
				context.closePath();
				context.drawImage(image, 0, 0, width, height);
				replaceSrc = canvas.toDataURL("image/jpeg"); //canvas转DataURL(base64格式)
		
				//将压缩后的路径 追加到临时路径数组中
				var totalList = [];
				if (currentImgTempArray.length > 0) {
					totalList = currentImgTempArray.concat(replaceSrc);
				} else {
					totalList[0] = replaceSrc;
				}		
		        totalList.forEach(function(image){
					putb64(image)
				}) 
			    
			};
		};
		
	})
};
function getProject(){
	var url = "/project/getProjectItems ";
	var data = {
		id: projectId
	}
	pushData(url, data, function(res) {
		if (res.state) {
			projectItems = res.data.projectItems;
		} else if (res.errorCode == 008) {
			window.location.href = url;
		} else {
			alert(res.message)
		}
	}.bind(this))
};
function getUserData() {
	var url = "/user/getUserById";
	var data = {
		client: 1,
		token: token,
		userId: parseInt(userId)
	}
	pushData(url, data, function(res) {
		if (res.state) {
			userData = res.data.user;
		} else if (res.errorCode == 008) {
			window.location.href = loginUrl;
		} else {
			alert(res.message)
		}
	})
};
function getQiniuToken(){
	var upData = {
		client: 1,
		token: token,
		userId: parseInt(userId)
	}
	pushData("/qiniu/get_token ", upData, function(res) {
		if (res.state) {
			qiniuData.token = res.data.result;
		} else if (res.errorCode == 008) {
			window.location.href = loginUrl;
		} else {
			alert(res.message)
		}
	}.bind(this));
};
function getQiniuImageUrl(){
	var upData = {
		client: 1,
		token: token,
		userId: parseInt(userId)
	}
	pushData("/qiniu/get_image_rul", upData, function(res) {
		if (res.state) {
			upload_qiniu_addr = res.data.url;
		} else if (res.errorCode == 008) {
			window.location.href = loginUrl;
			alert(res.message)
		}
	});
};	
function putb64(image){
	var pic = image.split('base64,')[1];
    var url = "http://up.qiniu.com/putb64/-1"; 
	var xhr = new XMLHttpRequest();
	var xhrget = new XMLHttpRequest();
	var obj;
	//xhr.onreadystatechange=function(){
	//	if (xhrget.readyState==4){
	//		 obj = JSON.parse(xhrget.responseText);
	//	}
	//}
	//xhrget.open("GET", "http://jssdk.demo.qiniu.io/uptoken", true); 
	   // xhrget.send();
	xhr.onreadystatechange=function(){
		if (xhr.readyState==4){
			//document.getElementById("myDiv").innerHTML=xhr.responseText;
			var el='<div class="box" style="margin-left: .6rem;">\
				<div class="image-c" style="margin: .2rem 0;">'+
				'<img src="'+image+'">'+
				
			'</div>\
			</div>'
			$('.up-load_image').append(el);
			$('.up-ico-box').hide()
			var r=JSON.parse(xhr.responseText)
			payImage.push(upload_qiniu_addr + "/" + r.key)
		}
	}
 
	xhr.open("POST", url, true); 
	xhr.setRequestHeader("Content-Type", "application/octet-stream"); 
	xhr.setRequestHeader("Authorization", "UpToken "+qiniuData.token); 
	xhr.send(pic);
};
function userBeRefund(){
	var refund = false
	userData.projectIdStatus.forEach(function(project) {
		if (project.status == '4') {
			refund = true
		}
	});
	return refund
};
function userCanApply(){
	var canApply = true
	if(userData.projectIdStatus!==null){
		userData.projectIdStatus.forEach(function(project) {
			//pos机并且  1 申请中   2 处理中 3 成功 不能申请   其余都能
			if(this.projectId==1){
				if (project.status == '1'||project.status == '2'||project.status == '3') {
					canApply = false
				}
			}
		});
	}
	return canApply
};
 function checkoutUser() {
	var have = false
	userData.projectIdStatus.forEach(function(project) {
		if (projectId==1&&project.status !== '4') {
			have = true
		}
	}.bind(this))
	return have
};
function firstStep(){
	if(!userData){
		alert("网络延迟，稍等")
		return
	}
	if(this.userCanApply()){
		$(".step-one").hide()
		$(".step-two").show()
	}else{
		alert("您正在申请或已是金种子矿主，无需再次申购")
		window.location.href = "/userBundle/myAlreadyApplyProject.html"
	}
	// if ((userData.projectIdStatus == null || userData.projectIdStatus.length == 0)) {
	// 	$(".step-one").hide()
	// 	$(".step-two").show()
	// } else if (this.userBeRefund()) {
	// 	$(".step-one").hide()
	// 	$(".step-two").show() //驳回继续申请
	// } else {		
	// 	if (this.checkoutUser()) {
	// 		alert("您正在申请或已是金种子矿主，无需再次申购")
	// 		window.location.href = "/userBundle/userCenter.mobile.html"
	// 	}
	// }
	
};
function secondStep(){
	$(".step-two").hide()
	$(".step-three").show()
};
function applyProjectItem(){
	if (this.payImage.length == 0) {
		alert("请上传付款凭证")
		return
	}
	if(isApplyProjectItem){
		return
	}
	isApplyProjectItem=true;
	var url = "/project/applyProjectItem";
	var data = {
		address: '',
		client: 1,
		doc: "",
		bankCardPositive: '', //正面
		bankCardNegative: '', //反面
		bankName: '',
		idPositive: '', //正面
		idNegative: '', //反面
		personIdImage: '',
		moneyImage: payImage[0],
		payAccount: '',
		payType: '',
		projectId: projectId,
		projectItemId: projectItems[0].id,
		telephone: '',
		idNo: '',
		bankNo: '',
		realName: '',
		token: token,
		userId: userId
	};	
	pushData(url, data, function(res) {
		isApplyProjectItem = false
		if (res.state) {
			// alert("申请成功，矿机正在激活中");
			// window.location.href = "/"
			$("#popup").show()
			$("#popupBackground").show()
			projectUserId=res.data.id;
		} else if (res.errorCode == 008) {
			window.location.href = loginUrl;
		} else {
			alert(res.message);
		}
	});
}
function goConfirmReslut(){
	window.location.href="/userBundle/identification.mobile.html#"+projectUserId
}
function closePopup(){
	$("#popup").hide()
	$("#popupBackground").hide()
	window.location.href="../index.mobile.html"
}
</script>
<div class="wap-apply-pos" id="uploadPayInfo" >
			<div class="wap-header">申请矿机
				<a class="back" onClick="javascript:window.history.back();return false;"></a>
			</div>
			<div class="step-one" >
				<div>
					<img src="../image/pos_image.png" />
				</div>
				<!-- <div class="info">*认购价格350元,认购款待交易达标后可申请秒退</div> -->
				<div class="next-btn">
					<div class="btn" onclick="firstStep() ">下一步</div>
				</div>
			</div>
			<div class="step-two" style="display: none;">
				<div class="info" style="margin-right: .3rem;line-height: .38rem;margin-top: 2rem;">*本人已清楚知晓吉祥三宝产品的相关功能与价格，同意支付相关款项</div>
				<div class="next-btn">
					<div class="btn" onclick="secondStep()">下一步</div>
				</div>
			</div>
			<div class="step-three"  style="padding-bottom: 1.6rem;display: none;" >
				<div class="image-c" style="width: 80%;margin: 0 auto;">
					<img src="../image/pay_new.png" />
				</div>
				<div class="info" style="margin-top: .1rem;font-size: .26rem;">
					*请付款，并提供付款凭证截图
					</br> 需要发票请在下一步填写发票抬头
					</br> 邮寄请在下一步填写地址（上门安装可不填）
				</div>
				<div class="item-c" style="margin-top: .3rem;">
					<div style="font-size:.3rem;margin: 0 .2rem 0 .6rem;font-weight: bold;align-items: center;display: flex;min-width: 1.5rem;">付款凭证</div>
					<div class="upload-wrap">
						<div class="box up-ico-box" style="margin-left: .6rem;">
							<label class="p dotted">
								<input type="file" accept="image/*" name="file" id="upload">
								<img src="../image/add_ico.png" alt="">
							</label>
						</div>
						<div class="up-load_image"></div>
							
						
					</div>
				</div>
				
				<div class="next-btn">
					<div class="btn" onclick ="applyProjectItem()">确定申请</div>
				</div>
				
			</div>
</div>			
<!--  压缩图片用的  -->
<canvas id="compressCanvas" style="position: fixed; z-index: -1; opacity: 0; top: -100%; left: -100%"></canvas>
<div class="pop-up-background" style="display: none;" id="popupBackground"></div>
<div class="pop-up-content" id="popup" style="display: none;">
	<div class="pop-up-info-c">恭喜您购机成，请立即实名认证完成激活</div>
	<div class="btn-c">
		<div class="cancel" onclick="closePopup()">稍后认证</div>
		<div class="confirm" onclick="goConfirmReslut()">立即认证</div>
	</div>
</div>

</body>
</html>